//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MultiAssetOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Option.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (Cephei::QL::IPayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COption(CMultiAssetOption::typeid)
{
    CPayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phMultiAssetOption = NULL;
#endif
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (new QuantLib::MultiAssetOption ( _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppMultiAssetOption)->setPricingEngine (_QL_Pricer);
        SetOption (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (boost::shared_ptr<QuantLib::MultiAssetOption>& childNative, Object^ owner) : COption(CMultiAssetOption::typeid)
{
#ifdef HANDLE
	_phMultiAssetOption = NULL;
#endif
	_ppMultiAssetOption = &childNative;
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
}
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (QuantLib::MultiAssetOption& childNative, Object^ owner) : COption(CMultiAssetOption::typeid)
{
#ifdef HANDLE
	_phMultiAssetOption = NULL;
#endif
	_ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (&childNative);
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
    _MultiAssetOptionOwner = owner;
    _OptionOwner = owner;
}

Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (CMultiAssetOption^ copy) : COption(CMultiAssetOption::typeid)
{
#ifdef HANDLE
	_phMultiAssetOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (copy->GetShared());
        _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
    }
}
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (System::Type^ t) : COption(CMultiAssetOption::typeid)
{
#ifdef HANDLE
	_phMultiAssetOption = NULL;
#endif
	if (!t->IsSubclassOf(CMultiAssetOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (QuantLib::Handle<QuantLib::MultiAssetOption>& childNative, Object^ owner)  : COption(CMultiAssetOption::typeid)
{
	_phMultiAssetOption = &childNative;
	_ppMultiAssetOption = &static_cast<boost::shared_ptr<QuantLib::MultiAssetOption>>(childNative.currentLink());
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
    _MultiAssetOptionOwner = owner;
}
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (QuantLib::Handle<QuantLib::MultiAssetOption> childNative)  : COption(CMultiAssetOption::typeid)
{
	_phMultiAssetOption = &childNative;
	_ppMultiAssetOption = &static_cast<boost::shared_ptr<QuantLib::MultiAssetOption>>(childNative.currentLink());
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CMultiAssetOption::CMultiAssetOption (QuantLib::MultiAssetOption childNative)  : COption(CMultiAssetOption::typeid)
{
#ifdef HANDLE
	_phMultiAssetOption = NULL;
#endif
	_ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (new QuantLib::MultiAssetOption (childNative));
    _ppOption = new boost::shared_ptr<QuantLib::Option> (boost::dynamic_pointer_cast<QuantLib::Option> (*_ppMultiAssetOption));
}
#endif

Cephei::QL::Instruments::CMultiAssetOption::~CMultiAssetOption ()
{
    if (_ppMultiAssetOption != NULL)
    {
	    delete _ppMultiAssetOption;
        _ppMultiAssetOption = NULL;
    }
}
Cephei::QL::Instruments::CMultiAssetOption::!CMultiAssetOption ()
{
    if (_ppMultiAssetOption != NULL)
    {
	    delete _ppMultiAssetOption;
    }
}
QuantLib::MultiAssetOption& Cephei::QL::Instruments::CMultiAssetOption::GetReference ()
{
    if (_ppMultiAssetOption == NULL) throw gcnew NativeNullException ();
	return **_ppMultiAssetOption;
}
boost::shared_ptr<QuantLib::MultiAssetOption>& Cephei::QL::Instruments::CMultiAssetOption::GetShared ()
{
    if (_ppMultiAssetOption == NULL) throw gcnew NativeNullException ();
	return *_ppMultiAssetOption;
}
QuantLib::MultiAssetOption* Cephei::QL::Instruments::CMultiAssetOption::GetPointer ()
{
    if (_ppMultiAssetOption == NULL) throw gcnew NativeNullException ();
	return &**_ppMultiAssetOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiAssetOption>& Cephei::QL::Instruments::CMultiAssetOption::GetHandle ()
{
	if (_phMultiAssetOption == NULL)
	{
		_phMultiAssetOption = new Handle<QuantLib::MultiAssetOption> (*_ppMultiAssetOption);
	}
	return *_phMultiAssetOption;
}
#endif
bool Cephei::QL::Instruments::CMultiAssetOption::HasNative () 
{
	return (_ppMultiAssetOption != NULL);
}

Double Cephei::QL::Instruments::CMultiAssetOption::Delta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->delta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CMultiAssetOption::DividendRho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->dividendRho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CMultiAssetOption::Gamma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->gamma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CMultiAssetOption::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMultiAssetOption)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CMultiAssetOption::Rho::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->rho ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CMultiAssetOption::Theta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->theta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CMultiAssetOption::Vega::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMultiAssetOption)->vega ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IMultiAssetOption^ Cephei::QL::Instruments::CMultiAssetOption_Factory::Create (Cephei::QL::IPayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CMultiAssetOption ( payoff,  exercise,  QL_Pricer);
}
